import { RequestHandler } from "express"
import { DataStore } from "../../data"
import { User } from "../../data/types"

export const apiUpdateUserInfor:RequestHandler=(req,res,next)=>{
    let index=DataStore.user.findIndex(item=>item.id== req.params.id)
    if(index==-1){
        res.status(404).json({ status: "failed", message: "user id NorFound" })
    } 
    else{
        let originalInfor=DataStore.user[index];
        let updarteInfor={
            id:req.params.id,
            userId:req.body.userId||originalInfor.userId,
            title:req.body.title||originalInfor.title,
            body:req.body.body||originalInfor.body,
            price:req.body.price||originalInfor.price,
            currency:req.body.currency||originalInfor.currency,
            imgs:req.body.imgs||originalInfor.imgs
        } as User
        DataStore.user[index]=updarteInfor;
        res.status(200).json({ status: "success", message: "updateSuccess" })
    }


}